package com.gome.wechat.program.configuration;


import static com.alibaba.druid.util.JdbcConstants.*;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by liwei113 on 2017/10/18 0018.
 */
@Configuration
public class DruidDataSourceConfig {

    private enum DriverType{
        MYSQL,MSSQL
    }

    @Bean
    @Primary
    @Qualifier("dataSource")
    public DataSource dataSource(@Value("${druid.datasource.primary.url}") String url,
                                 @Value("${druid.datasource.primary.username}") String username,
                                 @Value("${druid.datasource.primary.password}") String password,
                                 @Value("${druid.datasource.primary.initialSize}") int initialSize,
                                 @Value("${druid.datasource.primary.minIdle}") int minIdle,
                                 @Value("${druid.datasource.primary.maxActive}") int maxActive) throws SQLException {
        return getDataSource(DriverType.MYSQL,url, username, password,initialSize,minIdle,maxActive);
    }

    private DataSource getDataSource(DriverType driverType,String url, String username, String password,int initialSize,int minIdle,int maxActive) throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxActive(maxActive);

        switch (driverType){
            case MSSQL:
                dataSource.setDriverClassName(SQL_SERVER_DRIVER_SQLJDBC4);
                dataSource.setDbType(SQL_SERVER);
                break;

            case MYSQL:
                dataSource.setDriverClassName(MYSQL_DRIVER);
                dataSource.setDbType(MYSQL);
                break;
        }

        setConnectionPool(dataSource);

        return dataSource;
    }

    private void setConnectionPool(DruidDataSource dataSource) throws SQLException {

        dataSource.setMaxWait(60000);
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(300000);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestWhileIdle(true);
        dataSource.setFilters("stat,wall,slf4j");
        dataSource.setUseGlobalDataSourceStat(true);

        Properties properties = new Properties();
        properties.setProperty("druid.stat.mergeSql","true");
        properties.setProperty("druid.stat.slowSqlMillis","1000");
        dataSource.setConnectProperties(properties);
    }
}
